// test-task-group.js
import axios from 'axios';

async function testTaskGroupAPI() {
  const baseURL = 'http://localhost:3000';
  let authToken = '';
  
  try {
    // 先登录获取token
    console.log('Logging in to get auth token...');
    const loginResponse = await axios.post(`${baseURL}/api/v1/login`, {
      username: 'testuser',
      password: '123456'
    });
    
    authToken = loginResponse.data.data.token;
    console.log('Auth token obtained:', authToken.substring(0, 20) + '...');
    
    // 设置认证头部
    const authHeaders = {
      'Authorization': `Bearer ${authToken}`
    };
    
    // 测试创建任务分组
    console.log('\nTesting create task group...');
    const createResponse = await axios.post(`${baseURL}/api/v1/task-groups`, 
      { name: '工作任务' },
      { headers: authHeaders }
    );
    console.log('Create task group response:', createResponse.data);
    
    // 测试获取任务分组列表
    console.log('\nTesting get task groups...');
    const getResponse = await axios.get(`${baseURL}/api/v1/task-groups`, 
      { headers: authHeaders }
    );
    console.log('Get task groups response:', getResponse.data);
    
    // 测试更新任务分组
    if (getResponse.data.data && getResponse.data.data.length > 0) {
      const groupId = getResponse.data.data[0].group_id;
      console.log(`\nTesting update task group (ID: ${groupId})...`);
      const updateResponse = await axios.put(`${baseURL}/api/v1/task-groups/${groupId}`, 
        { name: '工作计划' },
        { headers: authHeaders }
      );
      console.log('Update task group response:', updateResponse.data);
      
      // 测试删除任务分组
      console.log(`\nTesting delete task group (ID: ${groupId})...`);
      const deleteResponse = await axios.delete(`${baseURL}/api/v1/task-groups/${groupId}`, 
        { headers: authHeaders }
      );
      console.log('Delete task group response:', deleteResponse.data);
    }
  } catch (error) {
    console.error('Task Group API Test Error:', error.response ? error.response.data : error.message);
  }
}

testTaskGroupAPI();